//
//  StatusCell.m
//  friendCyc
//
//  Created by roiland on 13-3-26.
//  Copyright (c) 2013年 sun. All rights reserved.
//

#import "StatusCell.h"
#import "FirCirLocalResourceDataSource.h"
#import "FirCirCommentDataSource.h"
#import "FirendCircleDataSource.h"
#import "FirCirPersonDataSource.h"

#import "sendFirCirDelNew.h"
#import "sendNewsLength.h"

#import "SpeexCodec.h"



@interface StatusCell()
- (void)BtnClicked:(id)sender;
- (void)ImageBtnClicked:(id)sender;
@end;

@implementation StatusCell

@synthesize status,imgPhoto,statusAnchor,statusContent,headPhoto,timeLabel;
@synthesize imgIntrLabel,commentsbtn,commentsCntLabel,delegate,soundBtn;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        statusContent = [[UILabel alloc]init];
        [statusContent setBackgroundColor:[UIColor clearColor]];
        [statusContent setFrame:CGRectMake(20, 28, 280, 220)];
        [statusContent setFont:[UIFont fontWithName:@"Arial" size:14]];
        [statusContent setLineBreakMode:NSLineBreakByTruncatingTail];
        [self addSubview:statusContent];
        
        soundBtn= [[UIImageView alloc] init];
        soundBtn.frame = CGRectMake(20, 48,
                                    STATUSCELL_SOUNDBTN_W,
                                    STATUSCELL_SOUNDBTN_W);
        soundBtn.image = [UIImage imageNamed:@"btn_audioPlay_press.png"];
        soundBtn.userInteractionEnabled = YES;
        UITapGestureRecognizer *singleTouchsb =
        [[UITapGestureRecognizer alloc] initWithTarget:self
                                                action:@selector(soundBtnClicked:)];
        [soundBtn addGestureRecognizer:singleTouchsb];
        [self addSubview:soundBtn];
        
        imgPhoto = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loadingImg.png"]];
        [imgPhoto setFrame:CGRectMake(20, 0, 64, 64)];
        imgPhoto.userInteractionEnabled = YES;
        UITapGestureRecognizer *singleTouch =
        [[UITapGestureRecognizer alloc] initWithTarget:self
                                                action:@selector(ImageBtnClicked:)];
        [imgPhoto addGestureRecognizer:singleTouch];
        [self addSubview:imgPhoto];
        
        headPhoto = [[UIImageView alloc]initWithFrame:CGRectMake(15, 5, 40, 40)];
        [headPhoto setImage:[UIImage imageNamed:@"touxiang_40x40.png"]];
        headPhoto.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *headTouch =
        [[UITapGestureRecognizer alloc] initWithTarget:self
                                                action:@selector(HeadBtnClicked:)];
        [headPhoto addGestureRecognizer:headTouch];
        
        [self addSubview:headPhoto];
        
        statusAnchor = [[UILabel alloc]initWithFrame:CGRectMake(60,5, 150, 30)];
        [statusAnchor setText:@""];
        [statusAnchor setFont:[UIFont fontWithName:@"Arial" size:14]];
        [statusAnchor setBackgroundColor:[UIColor clearColor]];
        [statusAnchor setTextColor:[UIColor brownColor]];
        [self addSubview:statusAnchor];
        
        timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(210,5, 200, 30)];
        [timeLabel setText:@""];
        [timeLabel setFont:[UIFont fontWithName:@"Arial" size:14]];
        [timeLabel setBackgroundColor:[UIColor clearColor]];
        [timeLabel setTextColor:[UIColor brownColor]];
        [self addSubview:timeLabel];
        
        imgIntrLabel = [[UILabel alloc]initWithFrame:CGRectMake(20,5, 200, 30)];
        [imgIntrLabel setText:@""];
        [imgIntrLabel setFont:[UIFont fontWithName:@"Arial" size:14]];
        [imgIntrLabel setBackgroundColor:[UIColor clearColor]];
        [imgIntrLabel setTextColor:[UIColor brownColor]];
        [self addSubview:imgIntrLabel];
        
        commentsbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [commentsbtn setFrame:CGRectMake(10,statusContent.frame.size.height-30,70,32)];
        [commentsbtn setTitleEdgeInsets:UIEdgeInsetsMake(20, 0, 0, -25)];
        [commentsbtn setTitle:@"发表评论" forState:UIControlStateNormal];
        [commentsbtn.titleLabel setFont:[UIFont fontWithName:@"Arial" size:14]];
        [commentsbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [commentsbtn addTarget:self action:@selector(BtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:commentsbtn];
        
        self.packupBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.packupBtn setFrame:CGRectMake(20,statusContent.frame.size.height + 5,70,32)];
        [self.packupBtn setTitleEdgeInsets:UIEdgeInsetsMake(20, 0, 0, -25)];
        [self.packupBtn setTitle:@"显示更多" forState:UIControlStateNormal];
        [self.packupBtn.titleLabel setFont:[UIFont fontWithName:@"Arial" size:14]];
        [self.packupBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [self.packupBtn addTarget:self action:@selector(PackupBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        self.packupBtn.hidden = YES;
        self.packupBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
        [self addSubview:self.packupBtn];
        
        
        commentsCntLabel = [[UILabel alloc]initWithFrame:CGRectMake(230,statusContent.frame.size.height-30,70,32)];
        [commentsCntLabel setText:@"评论:0条"];
        [commentsCntLabel setFont:[UIFont fontWithName:@"Arial" size:14]];
        [commentsCntLabel setBackgroundColor:[UIColor clearColor]];
        [commentsCntLabel setTextColor:[UIColor brownColor]];
        [self addSubview:commentsCntLabel];
        
        player = nil;
        
        UILongPressGestureRecognizer *recognizer =
        [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
        [recognizer setMinimumPressDuration:1.0f];
        [self addGestureRecognizer:recognizer];
        
        self.imageArr = [[NSMutableArray alloc] init];
        
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)ImageBtnClicked:(id)sender
{
  [delegate imageButtonClicked:imgPhoto.tag];
}

-(void)HeadBtnClicked:(id)sender
{
    if ([ViewManager sharedViewManager].currentVC ==
        (CommonViewController *)[ViewManager sharedViewManager].friendStatusListvc) {
        
        
        
        if (status != nil) {
            
            [DataControl shareControl].currentFI = [[DataControl shareControl] SearchInfo:status.mail];
            
            if ([DataControl shareControl].currentFI != nil) {
                [[DataControl shareControl].fcPersonDS setCurshow:
                 [[DataControl shareControl].fcPersonDS getShowArrByMail:
                  [DataControl shareControl].currentFI.mail]];
                
                [[netManager sharedNetManager].sendFCNewL SendNewsLength:[DataControl shareControl].currentFI.mail];
                
                [[ViewManager sharedViewManager] showView:VI_PERSONALSTATUS
                                         withIsRemoveCurV:false
                                            withDirection:VD_RIGHT];
            }
            
            
        }
        
    }
}

-(void)soundBtnClicked:(id)sender
{
    if (player != nil && [player isPlaying]) {
        [player stop];
        soundBtn.image = [UIImage imageNamed:@"btn_audioPlay_press.png"];
    }
    else
    {
    AVAudioSession *session = [AVAudioSession sharedInstance];
    NSError *sessionError;
    [session setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
    
    if(session == nil)
    {
        AS_LOG(@"Error creating session: %@", [sessionError description]);
    }
    else
        [session setActive:YES error:nil];
    NSError *playerError;
    NSData *SpeexData = [NSData dataWithContentsOfFile:
                         [[DataControl shareControl].fcLocalRecDS getRcLocalPathByID:status.statusId]];
    NSData *NewPCMData = DecodeSpeexToWAVE(SpeexData);
    player = [[AVAudioPlayer alloc] initWithData:NewPCMData error:&playerError];
        player.delegate = self;
    AS_LOG(@"voice length is %f",player.duration);
    [player prepareToPlay];
    [player play];
    
    soundBtn.image = [UIImage imageNamed:@"btn_audioPause_press.png"];
    }
}

- (void)BtnClicked:(id)sender
{
    [[DataControl shareControl].fccommentDS SetCommentSendInfo:status.mail
                                                     withNewID:status.statusId
                                                   withSpeakTo:status.mail];
    
    [[ViewManager sharedViewManager] showView:VI_COMMENT
                             withIsRemoveCurV:false
                                withDirection:VD_RIGHT];
}

- (void)resizeTheHeight
{
    if (status == nil) {
        return;
    }
    
    CGFloat contentWidth = 280;
    
    if (status.stIndex == FCG_ONE_SOUND) {
        soundBtn.frame = CGRectMake(20,
                                    STATUSCELL_INTERVAL + STATUSCELL_HEAD_HIGH,
                                    STATUSCELL_SOUNDBTN_W,
                                    STATUSCELL_SOUNDBTN_W);
        
        if (status.imageIDArray.count > 0) {
            if (status.imageIDArray.count == 1) {
                [imgPhoto setFrame:CGRectMake(20,
                                              STATUSCELL_INTERVAL * 2 +
                                              STATUSCELL_HEAD_HIGH +
                                              STATUSCELL_SOUNDBTN_W,
                                              72, 72)];
                
                NSString *picsrl = [status.imageIDArray objectAtIndex:0];
                NSString *imgUrl = [[DataControl shareControl].fcLocalRecDS getRcLocalPathByID:picsrl];
                if (![imgUrl isEqualToString:@""]) {
                    [imgPhoto setImage:[UIImage imageWithContentsOfFile:imgUrl]];
                    [self imageButtonLoadedImage:imgPhoto];
                }
                
                [imgIntrLabel setFrame:CGRectMake(20,
                                                  STATUSCELL_HEAD_HIGH +
                                                  STATUSCELL_INTERVAL * 3 +
                                                  STATUSCELL_SOUNDBTN_W +
                                                  imgPhoto.frame.size.height
                                                  ,200, 30)];
                
                [commentsCntLabel setFrame:CGRectMake(10,
                                                      STATUSCELL_HEAD_HIGH +
                                                      STATUSCELL_INTERVAL * 4 +
                                                      STATUSCELL_SOUNDBTN_W +
                                                      imgPhoto.frame.size.height +
                                                      30
                                                      ,70,32)];
                [commentsbtn setFrame:CGRectMake(230,
                                                 STATUSCELL_HEAD_HIGH +
                                                 STATUSCELL_INTERVAL * 4 +
                                                 STATUSCELL_SOUNDBTN_W +
                                                 imgPhoto.frame.size.height +
                                                 30
                                                 ,70,32)];
            }
            else
            {
                int iintval = 3;
                if (self.imageArr.count == 4) {
                    iintval = 2;
                }
                    for (int i = 0; i < self.imageArr.count; ++i) {
                        UIImageView *iv = [self.imageArr objectAtIndex:i];
                        if (iv != nil) {
                            
                            int irow = i % iintval;
                            int iline = i / iintval;
                            
                            iv.frame = CGRectMake(ARR_IMAGE_START + (ARR_IMAGE_WIDTH + ARR_IMAGE_INTERVAL) * irow,
                                                  STATUSCELL_INTERVAL * 2 +
                                                  STATUSCELL_HEAD_HIGH +
                                                  STATUSCELL_SOUNDBTN_W +
                                                  (ARR_IMAGE_INTERVAL + ARR_IMAGE_WIDTH) * iline,
                                                  ARR_IMAGE_WIDTH,
                                                  ARR_IMAGE_WIDTH);
                        }
                    }
                
                        int iimagerow = (self.imageArr.count - 1) / 3 + 1;
                        int iimagehight = (ARR_IMAGE_INTERVAL + ARR_IMAGE_WIDTH) * iimagerow - ARR_IMAGE_INTERVAL;
                        
                        [imgIntrLabel setFrame:CGRectMake(20,
                                                          STATUSCELL_HEAD_HIGH +
                                                          STATUSCELL_INTERVAL * 3 +
                                                          STATUSCELL_SOUNDBTN_W +
                                                          iimagehight
                                                          ,200, 30)];
                        
                        [commentsCntLabel setFrame:CGRectMake(10,
                                                              STATUSCELL_HEAD_HIGH +
                                                              STATUSCELL_INTERVAL * 4 +
                                                              STATUSCELL_SOUNDBTN_W +
                                                              iimagehight +
                                                              30
                                                              ,70,32)];
                        [commentsbtn setFrame:CGRectMake(230,
                                                         STATUSCELL_HEAD_HIGH +
                                                         STATUSCELL_INTERVAL * 4 +
                                                         STATUSCELL_SOUNDBTN_W +
                                                         iimagehight +
                                                         30
                                                         ,70,32)];
                    
            }
            
            
        }
        else
        {
            [imgPhoto setFrame:CGRectMake(20,70, 0, 0)];
            
            [imgIntrLabel setFrame:CGRectMake(20,
                                              STATUSCELL_HEAD_HIGH +
                                              STATUSCELL_INTERVAL * 3 +
                                              STATUSCELL_SOUNDBTN_W +
                                              imgPhoto.frame.size.height
                                              ,0, 0)];
            
            [commentsCntLabel setFrame:CGRectMake(10,
                                                  STATUSCELL_HEAD_HIGH +
                                                  STATUSCELL_INTERVAL * 2 +
                                                  STATUSCELL_SOUNDBTN_W +
                                                  imgPhoto.frame.size.height
                                                  ,70,32)];
            [commentsbtn setFrame:CGRectMake(230,
                                             STATUSCELL_HEAD_HIGH +
                                             STATUSCELL_INTERVAL * 2 +
                                             STATUSCELL_SOUNDBTN_W +
                                             imgPhoto.frame.size.height 
                                             ,70,32)];
        }
    }
    else
    {
        UIFont *font = [UIFont fontWithName:@"Arial" size:14];
        
        
        CGSize size = [statusContent.text sizeWithFont:font
                                     constrainedToSize:CGSizeMake(contentWidth, CGFLOAT_MAX)
                                         lineBreakMode:NSLineBreakByTruncatingTail];
        
            if (status.ipackuptype == PPT_SHOWMORE) {
                size = [statusContent.text sizeWithFont:font
                                      constrainedToSize:CGSizeMake(contentWidth, 120)
                                          lineBreakMode:NSLineBreakByTruncatingTail];
        
                self.packupBtn.hidden = NO;
                [self.packupBtn setTitle:@"显示更多" forState:UIControlStateNormal];
                
                statusContent.frame = CGRectMake(20,
                                                 STATUSCELL_INTERVAL + STATUSCELL_HEAD_HIGH
                                                 , 280, size.height);
                
                self.packupBtn.frame = CGRectMake(20,
                                                  STATUSCELL_HEAD_HIGH +
                                                  STATUSCELL_INTERVAL * 2 +
                                                  statusContent.frame.size.height,
                                                  70, 32);
                
                if (status.imageIDArray.count > 0) {
                    if (status.imageIDArray.count == 1) {
                        [imgPhoto setFrame:CGRectMake(20,
                                                      STATUSCELL_INTERVAL * 3 +
                                                      STATUSCELL_HEAD_HIGH +
                                                      32 +
                                                      statusContent.frame.size.height,
                                                      72, 72)];
                        
                        NSString *picsrl = [status.imageIDArray objectAtIndex:0];
                        NSString *imgUrl = [[DataControl shareControl].fcLocalRecDS getRcLocalPathByID:picsrl];
                        if (![imgUrl isEqualToString:@""]) {
                            [imgPhoto setImage:[UIImage imageWithContentsOfFile:imgUrl]];
                            [self imageButtonLoadedImage:imgPhoto];
                        }
                        
                        [imgIntrLabel setFrame:CGRectMake(20,
                                                          STATUSCELL_INTERVAL * 4 +
                                                          STATUSCELL_HEAD_HIGH +
                                                          32 +
                                                          statusContent.frame.size.height +
                                                          imgPhoto.frame.size.height
                                                          ,200, 30)];
                        
                        [commentsCntLabel setFrame:CGRectMake(10,
                                                              STATUSCELL_INTERVAL * 5 +
                                                              STATUSCELL_HEAD_HIGH +
                                                              32 +
                                                              statusContent.frame.size.height +
                                                              imgPhoto.frame.size.height +
                                                              30
                                                              ,70,32)];
                        [commentsbtn setFrame:CGRectMake(230,
                                                         STATUSCELL_INTERVAL * 5 +
                                                         STATUSCELL_HEAD_HIGH +
                                                         32 +
                                                         statusContent.frame.size.height +
                                                         imgPhoto.frame.size.height +
                                                         30
                                                         ,70,32)];

                    }
                    else
                    {
                        int iintval = 3;
                        if (self.imageArr.count == 4) {
                            iintval = 2;
                        }
                        for (int i = 0; i < self.imageArr.count; ++i) {
                            UIImageView *iv = [self.imageArr objectAtIndex:i];
                            if (iv != nil) {
                                
                                int irow = i % iintval;
                                int iline = i / iintval;
                                
                                iv.frame = CGRectMake(ARR_IMAGE_START + (ARR_IMAGE_WIDTH + ARR_IMAGE_INTERVAL) * irow,
                                                      STATUSCELL_INTERVAL * 3 +
                                                      STATUSCELL_HEAD_HIGH +
                                                      32 +
                                                      statusContent.frame.size.height +
                                                      (ARR_IMAGE_INTERVAL + ARR_IMAGE_WIDTH) * iline,
                                                      ARR_IMAGE_WIDTH,
                                                      ARR_IMAGE_WIDTH);
                            }
                        }
                        
                            int iimagerow = (self.imageArr.count - 1) / 3 + 1;
                            int iimagehight = (ARR_IMAGE_INTERVAL + ARR_IMAGE_WIDTH) * iimagerow - ARR_IMAGE_INTERVAL;
                            
                            [imgIntrLabel setFrame:CGRectMake(20,
                                                              STATUSCELL_INTERVAL * 4 +
                                                              STATUSCELL_HEAD_HIGH +
                                                              32 +
                                                              statusContent.frame.size.height +
                                                              iimagehight
                                                              ,200, 30)];
                            
                            [commentsCntLabel setFrame:CGRectMake(10,
                                                                  STATUSCELL_INTERVAL * 5 +
                                                                  STATUSCELL_HEAD_HIGH +
                                                                  32 +
                                                                  statusContent.frame.size.height +
                                                                  iimagehight +
                                                                  30
                                                                  ,70,32)];
                            [commentsbtn setFrame:CGRectMake(230,
                                                             STATUSCELL_INTERVAL * 5 +
                                                             STATUSCELL_HEAD_HIGH +
                                                             32 +
                                                             statusContent.frame.size.height +
                                                             iimagehight +
                                                             30
                                                             ,70,32)];
                        
                    }
                    
                }
                else
                {
                    [imgPhoto setFrame:CGRectMake(20,70, 0, 0)];
                    
                    [imgIntrLabel setFrame:CGRectMake(20,
                                                      STATUSCELL_INTERVAL * 4 +
                                                      STATUSCELL_HEAD_HIGH +
                                                      32 +
                                                      statusContent.frame.size.height +
                                                      imgPhoto.frame.size.height
                                                      ,0, 0)];
                    
                    [commentsCntLabel setFrame:CGRectMake(10,
                                                          STATUSCELL_INTERVAL * 4 +
                                                          STATUSCELL_HEAD_HIGH +
                                                          32 +
                                                          statusContent.frame.size.height +
                                                          imgPhoto.frame.size.height
                                                          ,70,32)];
                    [commentsbtn setFrame:CGRectMake(230,
                                                     STATUSCELL_INTERVAL * 4 +
                                                     STATUSCELL_HEAD_HIGH +
                                                     32 +
                                                     statusContent.frame.size.height +
                                                     imgPhoto.frame.size.height 
                                                     ,70,32)];

                }
                
            }
            else if (status.ipackuptype == PPT_PACKUP)
            {
                self.packupBtn.hidden = NO;
                [self.packupBtn setTitle:@"收起" forState:UIControlStateNormal];
                
                statusContent.frame = CGRectMake(20,
                                                 STATUSCELL_INTERVAL + STATUSCELL_HEAD_HIGH
                                                 , 280, size.height);
                
                self.packupBtn.frame = CGRectMake(20,
                                                  STATUSCELL_HEAD_HIGH +
                                                  STATUSCELL_INTERVAL * 2 +
                                                  statusContent.frame.size.height,
                                                  70, 32);
                
                if (status.imageIDArray.count > 0) {
                    if (status.imageIDArray.count == 1) {
                        [imgPhoto setFrame:CGRectMake(20,
                                                      STATUSCELL_INTERVAL * 3 +
                                                      STATUSCELL_HEAD_HIGH +
                                                      32 +
                                                      statusContent.frame.size.height,
                                                      72, 72)];
                        
                        NSString *picsrl = [status.imageIDArray objectAtIndex:0];
                        NSString *imgUrl = [[DataControl shareControl].fcLocalRecDS getRcLocalPathByID:picsrl];
                        if (![imgUrl isEqualToString:@""]) {
                            [imgPhoto setImage:[UIImage imageWithContentsOfFile:imgUrl]];
                            [self imageButtonLoadedImage:imgPhoto];
                        }
                        
                        [imgIntrLabel setFrame:CGRectMake(20,
                                                          STATUSCELL_INTERVAL * 4 +
                                                          STATUSCELL_HEAD_HIGH +
                                                          32 +
                                                          statusContent.frame.size.height +
                                                          imgPhoto.frame.size.height
                                                          ,200, 30)];
                        
                        [commentsCntLabel setFrame:CGRectMake(10,
                                                              STATUSCELL_INTERVAL * 5 +
                                                              STATUSCELL_HEAD_HIGH +
                                                              32 +
                                                              statusContent.frame.size.height +
                                                              imgPhoto.frame.size.height +
                                                              30
                                                              ,70,32)];
                        [commentsbtn setFrame:CGRectMake(230,
                                                         STATUSCELL_INTERVAL * 5 +
                                                         STATUSCELL_HEAD_HIGH +
                                                         32 +
                                                         statusContent.frame.size.height +
                                                         imgPhoto.frame.size.height +
                                                         30
                                                         ,70,32)];
                    }
                    else
                    {
                        int iintval = 3;
                        if (self.imageArr.count == 4) {
                            iintval = 2;
                        }
                        for (int i = 0; i < self.imageArr.count; ++i) {
                            UIImageView *iv = [self.imageArr objectAtIndex:i];
                            if (iv != nil) {
                                
                                int irow = i % iintval;
                                int iline = i / iintval;
                                
                                iv.frame = CGRectMake(ARR_IMAGE_START + (ARR_IMAGE_WIDTH + ARR_IMAGE_INTERVAL) * irow,
                                                      STATUSCELL_INTERVAL * 3 +
                                                      STATUSCELL_HEAD_HIGH +
                                                      32 +
                                                      statusContent.frame.size.height +
                                                      (ARR_IMAGE_INTERVAL + ARR_IMAGE_WIDTH) * iline,
                                                      ARR_IMAGE_WIDTH,
                                                      ARR_IMAGE_WIDTH);
                            }
                        }
                        
                            int iimagerow = (self.imageArr.count - 1) / 3 + 1;
                            int iimagehight = (ARR_IMAGE_INTERVAL + ARR_IMAGE_WIDTH) * iimagerow - ARR_IMAGE_INTERVAL;
                            
                            [imgIntrLabel setFrame:CGRectMake(20,
                                                              STATUSCELL_INTERVAL * 4 +
                                                              STATUSCELL_HEAD_HIGH +
                                                              32 +
                                                              statusContent.frame.size.height +
                                                              iimagehight
                                                              ,200, 30)];
                            
                            [commentsCntLabel setFrame:CGRectMake(10,
                                                                  STATUSCELL_INTERVAL * 5 +
                                                                  STATUSCELL_HEAD_HIGH +
                                                                  32 +
                                                                  statusContent.frame.size.height +
                                                                  iimagehight +
                                                                  30
                                                                  ,70,32)];
                            [commentsbtn setFrame:CGRectMake(230,
                                                             STATUSCELL_INTERVAL * 5 +
                                                             STATUSCELL_HEAD_HIGH +
                                                             32 +
                                                             statusContent.frame.size.height +
                                                             iimagehight +
                                                             30
                                                             ,70,32)];
                        
                    }
                    
                }
                else
                {
                    [imgPhoto setFrame:CGRectMake(20,70, 0, 0)];
                    
                    [imgIntrLabel setFrame:CGRectMake(20,
                                                      STATUSCELL_INTERVAL * 4 +
                                                      STATUSCELL_HEAD_HIGH +
                                                      32 +
                                                      statusContent.frame.size.height +
                                                      imgPhoto.frame.size.height
                                                      ,0, 0)];
                    
                    [commentsCntLabel setFrame:CGRectMake(10,
                                                          STATUSCELL_INTERVAL * 4 +
                                                          STATUSCELL_HEAD_HIGH +
                                                          32 +
                                                          statusContent.frame.size.height +
                                                          imgPhoto.frame.size.height 
                                                          ,70,32)];
                    [commentsbtn setFrame:CGRectMake(230,
                                                     STATUSCELL_INTERVAL * 4 +
                                                     STATUSCELL_HEAD_HIGH +
                                                     32 +
                                                     statusContent.frame.size.height +
                                                     imgPhoto.frame.size.height
                                                     ,70,32)];
                }
                
            }
            else
            {
                self.packupBtn.hidden = YES;
                
                statusContent.frame = CGRectMake(20,
                                                 STATUSCELL_INTERVAL + STATUSCELL_HEAD_HIGH
                                                 , 280, size.height);
                
                if (status.imageIDArray.count > 0) {
                    if (status.imageIDArray.count == 1) {
                        [imgPhoto setFrame:CGRectMake(20,
                                                      STATUSCELL_INTERVAL * 3 +
                                                      STATUSCELL_HEAD_HIGH +
                                                      statusContent.frame.size.height,
                                                      72, 72)];
                        
                        NSString *picsrl = [status.imageIDArray objectAtIndex:0];
                        NSString *imgUrl = [[DataControl shareControl].fcLocalRecDS getRcLocalPathByID:picsrl];
                        if (![imgUrl isEqualToString:@""]) {
                            [imgPhoto setImage:[UIImage imageWithContentsOfFile:imgUrl]];
                            [self imageButtonLoadedImage:imgPhoto];
                        }
                        
                        [imgIntrLabel setFrame:CGRectMake(20,
                                                          STATUSCELL_INTERVAL * 3 +
                                                          STATUSCELL_HEAD_HIGH +
                                                          statusContent.frame.size.height +
                                                          imgPhoto.frame.size.height
                                                          ,200, 30)];
                        
                        [commentsCntLabel setFrame:CGRectMake(10,
                                                              STATUSCELL_INTERVAL * 4 +
                                                              STATUSCELL_HEAD_HIGH +
                                                              statusContent.frame.size.height +
                                                              imgPhoto.frame.size.height +
                                                              30
                                                              ,70,32)];
                        [commentsbtn setFrame:CGRectMake(230,
                                                         STATUSCELL_INTERVAL * 4 +
                                                         STATUSCELL_HEAD_HIGH +
                                                         statusContent.frame.size.height +
                                                         imgPhoto.frame.size.height +
                                                         30
                                                         ,70,32)];
                    }
                    else
                    {
                        int iintval = 3;
                        if (self.imageArr.count == 4) {
                            iintval = 2;
                        }
                        for (int i = 0; i < self.imageArr.count; ++i) {
                            UIImageView *iv = [self.imageArr objectAtIndex:i];
                            if (iv != nil) {
                                
                                int irow = i % iintval;
                                int iline = i / iintval;
                                
                                iv.frame = CGRectMake(ARR_IMAGE_START + (ARR_IMAGE_WIDTH + ARR_IMAGE_INTERVAL) * irow,
                                                      STATUSCELL_INTERVAL * 3 +
                                                      STATUSCELL_HEAD_HIGH +
                                                      statusContent.frame.size.height +
                                                      (ARR_IMAGE_INTERVAL + ARR_IMAGE_WIDTH) * iline,
                                                      ARR_IMAGE_WIDTH,
                                                      ARR_IMAGE_WIDTH);
                            }
                        }
                        
                            int iimagerow = (self.imageArr.count - 1) / 3 + 1;
                            int iimagehight = (ARR_IMAGE_INTERVAL + ARR_IMAGE_WIDTH) * iimagerow - ARR_IMAGE_INTERVAL;
                            
                            [imgIntrLabel setFrame:CGRectMake(20,
                                                              STATUSCELL_INTERVAL * 3 +
                                                              STATUSCELL_HEAD_HIGH +
                                                              statusContent.frame.size.height +
                                                              iimagehight
                                                              ,200, 30)];
                            
                            [commentsCntLabel setFrame:CGRectMake(10,
                                                                  STATUSCELL_INTERVAL * 4 +
                                                                  STATUSCELL_HEAD_HIGH +
                                                                  statusContent.frame.size.height +
                                                                  iimagehight +
                                                                  30
                                                                  ,70,32)];
                            [commentsbtn setFrame:CGRectMake(230,
                                                             STATUSCELL_INTERVAL * 4 +
                                                             STATUSCELL_HEAD_HIGH +
                                                             statusContent.frame.size.height +
                                                             iimagehight +
                                                             30
                                                             ,70,32)];
                        
                    }
                    
                }
                else
                {
                    [imgPhoto setFrame:CGRectMake(20,70, 0, 0)];
                    
                    [imgIntrLabel setFrame:CGRectMake(20,
                                                      STATUSCELL_INTERVAL * 4 +
                                                      STATUSCELL_HEAD_HIGH +
                                                      statusContent.frame.size.height +
                                                      imgPhoto.frame.size.height
                                                      ,0, 0)];
                    
                    [commentsCntLabel setFrame:CGRectMake(10,
                                                          STATUSCELL_INTERVAL * 2 +
                                                          STATUSCELL_HEAD_HIGH +
                                                          statusContent.frame.size.height +
                                                          imgPhoto.frame.size.height
                                                          ,70,32)];
                    [commentsbtn setFrame:CGRectMake(230,
                                                     STATUSCELL_INTERVAL * 2 +
                                                     STATUSCELL_HEAD_HIGH +
                                                     statusContent.frame.size.height +
                                                     imgPhoto.frame.size.height
                                                     ,70,32)];
                }
            }
        
    }
}

- (void)reloadCell
{
    for (UIImageView *iv in self.imageArr) {
        if (iv != nil) {
            [iv removeFromSuperview];
        }
    }
    
    [self.imageArr removeAllObjects];
    
    self.statusContent.text = status.statusText;
    [timeLabel setText:[status TurnTimeString]];
    
    FirendInfo *fi = [[DataControl shareControl] SearchInfo:status.mail];
    if(fi != nil)
    {
        [statusAnchor setText:fi.name];
    }
    
    NSString *imageurl = [[DataControl shareControl] SearchHeadImage:status.mail];
    if (![imageurl isEqualToString:@""]) {
        [headPhoto setImage:[UIImage imageWithContentsOfFile:imageurl]];
    }
    
    if ([status.imageIDArray count]>0) {
        imgIntrLabel.text = [NSString stringWithFormat:@"共上传%d张图片",[status.imageIDArray count]];
    }else{
        imgIntrLabel.text = @"";
    }
    
    switch (status.stIndex) {
        case FCG_ONE_TEXT:
        {
            [statusContent setHidden:NO];
            [soundBtn setHidden:YES];
        }
            break;
            case FCG_ONE_SOUND:
        {
            [statusContent setHidden:YES];
            [soundBtn setHidden:NO];
            self.packupBtn.hidden = YES;
        }
            break;
        default:
            break;
    }
    
    commentsCntLabel.text = [NSString stringWithFormat:@"评论:%d条",status.commentsCount];
    
    
    if(status.imageIDArray.count > 1)
    {
        imgPhoto.hidden = YES;
        
        for (int i = 0; i < status.imageIDArray.count; ++i) {
            UIImageView *iiv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ARR_IMAGE_WIDTH, ARR_IMAGE_WIDTH)];
            NSString *strurl = [status.imageIDArray objectAtIndex:i];
            NSString *imgUrl = [[DataControl shareControl].fcLocalRecDS getRcLocalPathByID:strurl];
            if (![imgUrl isEqualToString:@""]) {
                [iiv setImage:[UIImage imageWithContentsOfFile:imgUrl]];
            }
                else
                {
                    [iiv setImage:[UIImage imageNamed:@"loadingImg.png"]];
                }
                [iiv setContentScaleFactor:[[UIScreen mainScreen] scale]];
                iiv.contentMode =  UIViewContentModeScaleAspectFill;
                iiv.clipsToBounds  = YES;
                iiv.tag = i;
                iiv.userInteractionEnabled = YES;
                UITapGestureRecognizer *singleTouch =
                [[UITapGestureRecognizer alloc] initWithTarget:self
                                                        action:@selector(ImageArrBtnClicked:)];
                [iiv addGestureRecognizer:singleTouch];
                
                [self.imageArr addObject:iiv];
                
                [self.contentView addSubview:iiv];
            
        }
    }
    else if(status.imageIDArray.count == 1)
    {
        imgPhoto.hidden = NO;
    }
    
    [self resizeTheHeight];
}

-(void)ImageArrBtnClicked:(id)sender
{
    NSInteger index = [(UIGestureRecognizer *)sender view].tag;
    [delegate ImageArrBtnClicked:imgPhoto.tag withImageTag:index];
}

#pragma mark - delegate

- (void)imageButtonLoadedImage:(UIImageView*)imageButton
{
    UIImage *image = imageButton.image;
    CGFloat w = 1.0f;
    CGFloat h = 1.0f;
    if (image.size.width>280) {
        w = image.size.width/280;
    }
    if (image.size.height>72) {
        h = image.size.height/72;
    }
    CGFloat scole = w>h ? w:h;
    
    CGRect rect = CGRectMake(30 ,imageButton.frame.origin.y,image.size.width/scole,image.size.height/scole);
    [imgPhoto setFrame:rect];
    
}

- (void)imageButtonFailedToLoadImage:(EGOImageButton*)imageButton error:(NSError*)error;
{
    [imageButton cancelImageLoad];
}

#pragma mark AVAudioPlayerDelegate

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)myplayer successfully:(BOOL)flag{
        [myplayer stop];
        soundBtn.image = [UIImage imageNamed:@"btn_audioPlay_press.png"];
}

#pragma mark -
#pragma mark UIGestureRecognizer-Handling

- (void)handleLongPress:(UILongPressGestureRecognizer *)longPressRecognizer {
    if (longPressRecognizer.state != UIGestureRecognizerStateBegan) {
        return;
    }
    
    if ([self becomeFirstResponder] == NO) {
        return;
    }
    
    UIMenuItem *flag = [[UIMenuItem alloc] initWithTitle:@"复制文本" action:@selector(flag:)];
    
    
    UIMenuController *menu = [UIMenuController sharedMenuController];
    
    [menu setMenuItems:[NSArray arrayWithObjects:flag, nil]];
    [menu setTargetRect:self.statusContent.frame inView:self];
    
    [menu setMenuVisible:YES animated:YES];
    
}

-(BOOL)canBecomeFirstResponder {
    return YES;
}

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    if (action == @selector(flag:)) {
        if (status != nil) {
            if (status.stIndex == FCG_ONE_TEXT) {
                return YES;
            }
            else
            {
                return NO;
            }
        }
        else
        {
            return NO;
        }
    }if (action == @selector(delete:)) {
        if (status != nil) {
            if ([status.mail isEqualToString:[DataControl shareControl].personDS.uinfo.mail]
                && [ViewManager sharedViewManager].currentVC !=
                (CommonViewController *)[ViewManager sharedViewManager].commentsListvc) {
                return YES;
            }
            else
            {
                return NO;
            }
        }
            return YES;
    }
    else {
        return [super canPerformAction:action withSender:sender];
    }
}

- (void)delete:(id)sender
{
    if (status != nil) {
        [DataControl shareControl].fircirDS.strdelid = status.statusId;
        [[DataControl shareControl].LCDS addLogicCommand:LC_FIR_DELNEW];
        
        if ([ViewManager sharedViewManager].currentVC ==
            (CommonViewController *)[ViewManager sharedViewManager].friendStatusListvc)
        {
            [[DataControl shareControl].UICommendDS addUICommand:UC_FIRCIR_PYQ_BEGINDEL];
        }
        else if([ViewManager sharedViewManager].currentVC ==
                (CommonViewController *)[ViewManager sharedViewManager].personalStatusvc)
        {
            [[DataControl shareControl].UICommendDS addUICommand:UC_FIRCIR_PER_BEGINDEL];
        }
        
        [[netManager sharedNetManager].sendFCDelNew sendFCDelNew:status.statusId];
    }
}

-(void)flag:(id)sender {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    [pasteboard setString:statusContent.text];
}

-(void)PackupBtnClicked
{
    if (status == nil) {
        return;
    }
    
    if (status.ipackuptype == PPT_PACKUP) {
        [self.packupBtn setTitle:@"显示更多" forState:UIControlStateNormal];
        status.ipackuptype = PPT_SHOWMORE;
        
        [DataControl shareControl].fircirDS.bPackup = true;
    }
    else if(status.ipackuptype == PPT_SHOWMORE)
    {
        [self.packupBtn setTitle:@"收起" forState:UIControlStateNormal];
        status.ipackuptype = PPT_PACKUP;
        
        [DataControl shareControl].fircirDS.bPackup = false;
    }
    
    [DataControl shareControl].fircirDS.iPackupRow = self.irow;
    [DataControl shareControl].fircirDS.iPackupSection = self.isection;
    
    [[DataControl shareControl].UICommendDS addUICommand:UC_FIRCIR_STATE_PACKUP];
}

#pragma mark UIGestureRecognizerDelegate methods
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return YES;
}

@end
